package com.lance.answer.gui.answer.gk.answer.lms.export;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.Item;
import com.lance.answer.dto.XjCourseVideoInfo;
import com.lance.answer.gui.answer.Question;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.LmsOuchnVideoInfo;
import com.lance.answer.gui.answer.gk.answer.lms.LmsVideoInfo;
import com.lance.answer.gui.answer.gk.answer.lms.dto.LmsExamInfo;
import com.lance.answer.util.HttpUtil;
import com.lance.answer.util.MD5Utils;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.time.Duration;
import java.util.*;

/**
 * <p>获取某门课程所有形考的信息<p>
 * <p>
 * <p>
 * <p>
 * <p>
 * 工程经济,size:1
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/7/15 14:47
 */
public class ExportLmsExamInfo {


    public static final String JS = "var video = document.getElementsByTagName(\"video\");if(video.length>0){ video[0].src = \"https://jvod.300hu.com/vod/product/a7d50669-f57b-4da9-8cf8-43d04372cab0/0411e189a55b41cfada876041fa7fdd7.mp4?source=2&h265=h265/18799/d7d0ab77577f4ea0bcf5bc794316045a.mp4\";}";


    public static void main(String[] args) throws Exception {
        String driverPath = "d:\\ynouAutoStudy\\chromedriver.exe";
        ChromeDriver driver = Util.getDriver(driverPath);
        String url = "https://menhu.pt.ouchn.cn/site/ouchnPc/index";
        driver.get(url);
        Thread.sleep(3000);

        String userName = "2242001200093";
        String pwd = "Ouchn@2021";
//        String subjectName = "企业现场管理基础";
//        Long courseId = 16502l;
        String subjectName =
                "法律文化";
        Long courseId = 16796L;

        WebElement userNameInput = driver.findElement(By.id("loginName"));
        userNameInput.clear();
        userNameInput.sendKeys(userName);
        //TimeUnit.SECONDS.sleep(1);

        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.clear();
        passwordInput.sendKeys(pwd);
        //TimeUnit.SECONDS.sleep(1);

        // FIXME 要输入验证码
        Thread.sleep(10000);

        //webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginId"))).sendKeys(username);

        WebElement loginBtn = driver.findElement(By.name("button"));

        loginBtn.click();

        Thread.sleep(10000);



        // 进入课程首页
        // div className = ouchnPc_index_course w
        // div className = ouchnPc_index_course_div

        for (WebElement we : driver.findElements(By.cssSelector("div[class='ouchnPc_index_course_div']"))) {
            String textContent = we.getText();
            System.err.println("text:" + textContent);
            if (textContent.contains(subjectName)) {
                we.findElement(By.tagName("a")).click();
                break;
            }
        }
        Util.switchToLast(driver);

        List<LmsExamInfo> lmsExamInfoList = ExportLmsExamInfo.getUserCourseInfo(driver, courseId);

        List<LmsExamInfo.ExamsDTO> exams = Lists.newArrayList();
        lmsExamInfoList.forEach(e->exams.addAll(e.getExams()));

       for(LmsExamInfo.ExamsDTO examsDTO: exams){
         System.err.println("urlTasks.add(\"https://lms.ouchn.cn/course/"+courseId+"/learning-activity#/exam/"+examsDTO.getId()+"\");");
       }


        // TODO 现在开始获取具体课程所有的答题链接：：




       // https://lms.ouchn.cn/api/courses/13949/exam-list?conditions={"itemsSortBy":{"predicate":"created_at","reverse":true}}&page=1&page_size=20&reloadPage=false

        Thread.sleep(500000000);

//
    }



    public static  List<LmsExamInfo> getUserCourseInfo(WebDriver driver, Long courseId) throws Exception {

        // getPages
        int page = 1;
        int pageSize = 20;
        boolean hasNextPage = true;
        List<LmsExamInfo> lmsExamInfoList = Lists.newArrayList();
        while (hasNextPage) {

            String courseUrl = "https://lms.ouchn.cn/course/" + courseId + "/ng#/exam";
            driver.get(courseUrl);

            Thread.sleep(10000);
            String cookie = Util.getCookie(driver.manage().getCookies());

            String getUrl = "https://lms.ouchn.cn/api/courses/"+courseId+"/exam-list?conditions={\"itemsSortBy\":{\"predicate\":\"created_at\",\"reverse\":true}}" +
                    "&page="+page+"&page_size="+pageSize+"&reloadPage=false";

            Map<String, String> heads = Maps.newHashMap();
            heads.put("referer", "https://lms.ouchn.cn/course/" + courseId + "/ng");
            heads.put(":method", "GET");
            heads.put(":authority", "lms.ouchn.cn");
            heads.put("cookie", cookie);
            heads.put(":path", "/api/courses/"+courseId+"/exam-list?conditions=%7B%22itemsSortBy%22:%7B%22predicate%22:%22created_at%22,%22reverse%22:true%7D%7D&page=1&page_size=20&reloadPage=false");
            heads.put("accept", "application/json, text/plain, */*");

            String result = HttpUtil.getLmsOuchn(getUrl, cookie, heads);
            LmsExamInfo lmsExamInfo =  JSON.parseObject(result, LmsExamInfo.class);
            lmsExamInfoList.add(lmsExamInfo);
            if (page >= lmsExamInfo.getPages()) {
                hasNextPage = false;
            } else {
                //  hasNextPage = false;
                page++;
            }
        }

        return lmsExamInfoList;
    }

    }
